package com.riekoff.particlecubeworld.editor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cc.creativecomputing.control.CCControl;
import cc.creativecomputing.graphics.texture.CCTexture.CCTextureFilter;
import cc.creativecomputing.graphics.texture.CCTexture.CCTextureTarget;
import cc.creativecomputing.graphics.texture.CCTexture2D;
import cc.creativecomputing.graphics.texture.CCTextureData;
import cc.creativecomputing.graphics.texture.CCTextureIO;
import cc.creativecomputing.io.CCIOUtil;

public class AControllableColorMap extends AColorMap{
	
	private CCTextureData _myScheme;
	
	private class AZoneControls{
		int _myZone;
		
		public AZoneControls(int theZone){
			_myZone = theZone;
		}
		
		@CCControl(name = "color 0", min = 0, max = 49)
		private int _cColor0 = 0;
		@CCControl(name = "color 0 prob", min = 0, max = 1f)
		private float _cColor0Prob = 0;
		
		@CCControl(name = "color 1", min = 0, max = 49)
		private int _cColor1 = 0;
		@CCControl(name = "color 1 prob", min = 0, max = 1f)
		private float _cColor1Prob = 0;
		
		@CCControl(name = "color 2", min = 0, max = 49)
		private int _cColor2 = 0;
		@CCControl(name = "color 2 prob", min = 0, max = 1f)
		private float _cColor2Prob = 0;
	}
	
	private class AZonesControl{
		@CCControl(name = "zone 0")
		private AZoneControls _cZone0 = new AZoneControls(0);
		@CCControl(name = "zone 1")
		private AZoneControls _cZone1 = new AZoneControls(1);
		@CCControl(name = "zone 2")
		private AZoneControls _cZone2 = new AZoneControls(2);
		@CCControl(name = "zone 3")
		private AZoneControls _cZone3 = new AZoneControls(3);
		@CCControl(name = "zone 4")
		private AZoneControls _cZone4 = new AZoneControls(4);
		

		private AZoneControls[] _myZoneControls = new AZoneControls[]{_cZone0, _cZone1, _cZone2, _cZone3, _cZone4};
	}
	
	public static enum AZoneMode{
		DIRECT, FILE
	}
	
	@CCControl(name = "mode")
	private AZoneMode _cMode = AZoneMode.DIRECT;
	
	@CCControl(name = "scheme index", min = 0, max = 20)
	private int _cSchemeIndex = 0;
	
	private AZonesControl _myZonesControl = new AZonesControl();
	
	private List<CCTexture2D> _myColorSchemes = new ArrayList<>();
	private List<String> _myColorFiles = new ArrayList<>();
	
	public AControllableColorMap(CCTextureData theScheme){
		super(5);
		_myScheme = theScheme;
		
		for(String myFile : CCIOUtil.list("data/colorschemes") ){
			String myImageFile = "data/colorschemes/" + myFile;
			CCTexture2D myTexture = new CCTexture2D(CCTextureIO.newTextureData(myImageFile), CCTextureTarget.TEXTURE_RECT);
			myTexture.textureFilter(CCTextureFilter.NEAREST);
			_myColorSchemes.add(myTexture);
			_myColorFiles.add(myImageFile);
		}
	}
	
	public AZonesControl zonesControl(){
		return _myZonesControl;
	}
	
	private String _myOutPath;
	
	public String filePath(){
		switch(_cMode){
		case DIRECT:
			if(_myOutPath == null)saveTexture();

			int myStart = _myOutPath.indexOf("data" + File.separator + "colorschemes");
			if(myStart < 0)return null;
			
			return _myOutPath.substring(myStart);
		case FILE:
			if(_cSchemeIndex < _myColorSchemes.size()){
				return _myColorFiles.get(_cSchemeIndex);
			}
			return null;
		}
		return null;
	}
	
	@CCControl(name = "save")
	public void saveTexture(){
		if(!AFigurenEditor._cSaveActive){
			return;
		}
		CCIOUtil.createPath(CCIOUtil.appPath("data/colorschemes/color.bla"));
		_myOutPath =  CCIOUtil.selectOutput("save color map", CCIOUtil.appPath("data/colorschemes/"));
		if(_myOutPath == null)return;
		CCTextureIO.write(createData(true),_myOutPath);
	}

	public void update(float theDeltaTime){
		if(_cMode != AZoneMode.DIRECT)return;
		clear();
		for(AZoneControls myControl:_myZonesControl._myZoneControls){
			float myZoneProb = myControl._cColor0Prob + myControl._cColor1Prob + myControl._cColor2Prob;
			if(myZoneProb <= 0){
				addColor(myControl._myZone, _myScheme.getPixel(myControl._cColor0, 0), 1);
			}else{
				addColor(myControl._myZone, _myScheme.getPixel(myControl._cColor0, 0), myControl._cColor0Prob);
				addColor(myControl._myZone, _myScheme.getPixel(myControl._cColor1, 0), myControl._cColor1Prob);
				addColor(myControl._myZone, _myScheme.getPixel(myControl._cColor2, 0), myControl._cColor2Prob);
			}
		}
		texture();
	}
	
	@Override
	public CCTexture2D texture() {
		switch(_cMode){
		case DIRECT:
			return super.texture();
		case FILE:
			if(_cSchemeIndex < _myColorSchemes.size()){
				return _myColorSchemes.get(_cSchemeIndex);
			}
			break;
		}
		return super.texture();
	}
	
	
	
}
